package com.web.controller;

import com.core.shared.file.FTPHandle;
import com.core.shared.ImageUtils;
import com.service.LxfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.AbstractController;
import sun.awt.AppContext;
import sun.awt.SunToolkit;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-12-29
 * Time: 下午3:53
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/public")
public class PublicController {

    @Autowired
    private LxfService lxfService;


    @RequestMapping(value = "image/{guid}/{width}/{height}", method = RequestMethod.GET)
    public void testImage(@PathVariable String guid, @PathVariable String width, @PathVariable String height, HttpServletResponse response) {
        try {       //指定尺寸缩放
            byte[] contents = lxfService.showImageByGuid(guid);
            InputStream is = new ByteArrayInputStream(contents);
            if (AppContext.getAppContext() == null) {
                SunToolkit.createNewAppContext();
            }
            BufferedImage sourceImage = ImageIO.read(is);
            ImageUtils.resize(Integer.parseInt(width), Integer.parseInt(height), sourceImage, response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "ftp_upload", method = RequestMethod.POST)
    @ResponseBody
    public Object ftpUpload(MultipartFile file) {
        InputStream is = null;
        try {
            is = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        boolean b = FTPHandle.uploadFile("/", file.getOriginalFilename(), is);
        return b;
    }

    @RequestMapping(value = "ftp_download", method = RequestMethod.GET)
    public void ftpDownload() {
        FTPHandle.downloadFile("/", "2.png", "D:\\");
    }

    @RequestMapping(value = "base_encode", method = RequestMethod.POST)
    @ResponseBody
    public Object base64Encode(MultipartFile file) {
        String s = "";
        try {
            s = ImageUtils.base64Encoder(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    @RequestMapping(value = "base_decode", method = RequestMethod.POST)
    public void base64Decode(String decodeString, HttpServletResponse response) {
        try {
            OutputStream out = response.getOutputStream();
            byte[] bytes = ImageUtils.base64Decoder(decodeString);
            out.write(bytes);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "resize_upload", method = RequestMethod.POST)
    public Object resizeUpload(MultipartFile file) {
        if (file != null) {
            try {
                BufferedImage sourceImage = ImageIO.read(file.getInputStream());
                String fileName = file.getOriginalFilename();
                fileName = fileName.substring(0, fileName.lastIndexOf(".") + 1);
                File target = new File("f:/" + fileName + ".png");
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
                ImageUtils.resize(160, 160, sourceImage, bos);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}
